package dm.com.dmexamples.image_chooser;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;

import dm.com.dmexamples.App;
import dm.com.dmexamples.R;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class ViewUtil {

    /**
     * 切换视图的显示
     *
     * @param v 切换后可见为true else false
     */
    public static boolean toggle(View v) {
        if (v.getVisibility() == View.GONE) {
            v.setVisibility(View.VISIBLE);
            return true;
        } else {
            v.setVisibility(View.GONE);
            return false;
        }
    }

    public interface ToggleCount {
        /**
         * @param count 由 1 开始
         * @return false 执行第一次
         */
        public boolean atCount(Integer count);

    }

    /**
     * source 切换切换视图的显示
     *
     * @param source
     * @param target
     */
    public static void setToggle(View source, final View target) {
        source.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                toggle(target);
            }
        });
    }

    public interface RoundToggle {
        /**
         * @param source
         * @param target
         * @param visiable target的可见型
         */
        public void before(View source, final View target, boolean visiable);

        public void after(View source, final View target, boolean visiable);
    }

    public static void setToggle(final View source, final View target, final RoundToggle round) {
        source.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (target.getVisibility() == View.VISIBLE) {
                    round.before(source, target, true);
                } else {
                    round.before(source, target, false);
                }
                round.after(source, target, toggle(target));
            }
        });
    }

    public static void setToggleAndPerform(View source, final View target) {
        source.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                toggle(target);
            }
        });
        source.performClick();
    }

    public static void setToggleAndPerform(final View source, final View target, final RoundToggle round) {
        source.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (target.getVisibility() == View.VISIBLE) {
                    round.before(source, target, true);
                } else {
                    round.before(source, target, false);
                }
                round.after(source, target, toggle(target));
            }
        });
        source.performClick();
    }

    /**
     * 对象绑定视图
     *
     * @param v
     * @param value
     */
    public static void bindView(View v, Object value) {
        if (v == null || value == null)
            return;
        if (v instanceof TextView) {
            if (value instanceof CharSequence) {
                ((TextView) v).setText((CharSequence) value);
            } else {
                ((TextView) v).setText(value.toString());
            }
        }
        if (v instanceof ImageView) {
            if (value instanceof String) {
                Glide.with(App.getInstance()).load((String) value).centerCrop().crossFade().placeholder(R.mipmap.ic_launcher).into((ImageView) v);
            } else if (value instanceof Drawable) {
                ((ImageView) v).setImageDrawable((Drawable) value);
            } else if (value instanceof Bitmap) {
                ((ImageView) v).setImageBitmap((Bitmap) value);
            } else if (value instanceof Integer) {
                ((ImageView) v).setImageResource((Integer) value);
            }
        }
    }

    /**
     * 对象绑定视图,同时通过全局的数据修复
     *
     * @param v
     * @param value
     */
    public static void bindView(View v, Object value, String type) {
        if (v == null || value == null)
            return;
        if (v instanceof TextView) {
            if (value instanceof CharSequence) {
                ((TextView) v).setText((CharSequence) value);
            } else {
                ((TextView) v).setText(value.toString());
            }
        }
        if (v instanceof ImageView) {
            if (value instanceof String) {
                Glide.with(App.getInstance()).load((String) value).centerCrop().crossFade().into((ImageView) v);
            } else if (value instanceof Drawable) {
                ((ImageView) v).setImageDrawable((Drawable) value);
            } else if (value instanceof Bitmap) {
                ((ImageView) v).setImageBitmap((Bitmap) value);
            } else if (value instanceof Integer) {
                ((ImageView) v).setImageResource((Integer) value);
            }
        }
    }

    public static void bindingImageView(ImageView view, String url, boolean isPlaceholder, boolean isCenterCrop) {
        if (isPlaceholder && isCenterCrop) {
            Glide.with(App.getInstance()).load(url).centerCrop().crossFade().into(view);
        }
        if (!isPlaceholder && isCenterCrop) {
            Glide.with(App.getInstance()).load(url).centerCrop().crossFade().into(view);
        }
        if (!isCenterCrop && isPlaceholder) {
            Glide.with(App.getInstance()).load(url).crossFade().into(view);
        }
        if (!isCenterCrop && !isPlaceholder) {
            Glide.with(App.getInstance()).load(url).crossFade().into(view);
        }
    }

    public static void bindRoundImageView(@NonNull final ImageView view, String url, int radius) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(url)
//				.fitCenter()
                .crossFade()
                .bitmapTransform(new RoundedCornersTransformation(context, radius, 0, RoundedCornersTransformation.CornerType.ALL))
                .into(view);
    }

    public static void bindRoundImageView(@NonNull final ImageView view, int rid, int radius) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(rid)
//				.fitCenter()
                .crossFade()
                .bitmapTransform(new RoundedCornersTransformation(context, radius, 0, RoundedCornersTransformation.CornerType.ALL))
                .into(view);
    }

    /**
     * 设置模糊图片
     */
    public static void bindBlurImageView(@NonNull ImageView view, String url, int blur) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(url)
                .centerCrop()
                .bitmapTransform(new BlurTransformation(context, blur))
                .into(view);
    }

    public static void bindImageViewNoAnimal(@NonNull ImageView view, String url) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(url)
                .dontAnimate()
                .into(view);
    }


    public static void bindHeaderView(View v, Object value) {
        if (v == null || value == null)
            return;
        if (v instanceof ImageView) {
            if (value instanceof String) {
                Glide.with(App.getInstance()).load((String) value).centerCrop().crossFade().into((ImageView) v);
            } else if (value instanceof Drawable) {
                ((ImageView) v).setImageDrawable((Drawable) value);
            } else if (value instanceof Bitmap) {
                ((ImageView) v).setImageBitmap((Bitmap) value);
            } else if (value instanceof Integer) {
                ((ImageView) v).setImageResource((Integer) value);
            }
        }

    }

    /**
     * 添加圆形imageView
     */
    public static void bindCircleImageView(@NonNull ImageView view, String url) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(url)
                .centerCrop()
                .priority(Priority.HIGH)
                .crossFade()
                .bitmapTransform(new CropCircleTransformation(context))
                .into(view);
    }

    /**
     * 添加圆形imageView
     */
    public static void bindCircleImageView(@NonNull ImageView view, int url) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(url)
                .centerCrop()
                .priority(Priority.HIGH)
                .crossFade()
                .bitmapTransform(new CropCircleTransformation(context))
                .into(view);
    }

    /**
     * 添加圆角imageView
     */
    public static void bindRoundImageView(@NonNull ImageView view, String url) {
        Context context = App.getInstance();
        Glide.with(context)
                .load(url)
                .centerCrop()
                .priority(Priority.HIGH)
                .crossFade()
                .bitmapTransform(new RoundedCornersTransformation(context, 10, 0))
                .into(view);
    }

    public static boolean isEmpty(TextView... texts) {
        if (texts == null)
            return false;
        for (int i = 0; i < texts.length; i++) {
            boolean empty = TextUtils.isEmpty(texts[i].getText().toString().trim());
            if (empty) {
                return empty;
            }
        }
        return false;
    }

    public static void bindcrcleView(View v, Object value, String type) {
        if (v == null || value == null)
            return;
        if (v instanceof TextView) {
            if (value instanceof CharSequence) {
                ((TextView) v).setText((CharSequence) value);
            } else {
                ((TextView) v).setText(value.toString());
            }
        }
        if (v instanceof ImageView) {
            if (value instanceof String) {
                Glide.with(App.getInstance())
                        .load((String) value)
                        .centerCrop()
                        .priority(Priority.HIGH)
                        .crossFade()
                        .bitmapTransform(new CropCircleTransformation(App.getInstance()))
                        .into((ImageView) v);
            } else if (value instanceof Drawable) {
                ((ImageView) v).setImageDrawable((Drawable) value);
            } else if (value instanceof Bitmap) {
                ((ImageView) v).setImageBitmap((Bitmap) value);
            } else if (value instanceof Integer) {
                ((ImageView) v).setImageResource((Integer) value);
            }
        }
    }


}
